package com.sunshine.lnuplus.ui.fragment

import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.android.material.textfield.TextInputLayout
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.adapter.CountDownRecyclerAdapter
import com.sunshine.lnuplus.base.BaseFragment
import com.sunshine.lnuplus.model.room.CountDown
import com.sunshine.lnuplus.utils.DateUtils
import com.sunshine.lnuplus.view.DialogWithView
import com.sunshine.lnuplus.viewModel.CountDownViewModel
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.fragment_count_down.*

class CountDownFragment : BaseFragment() {

    private lateinit var viewModel: CountDownViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_count_down, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(CountDownViewModel::class.java)

        initView()
    }

    private fun initView() {
        if(getTimetableTheme() == 1 && !context!!.getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
            count_down_text.setTextColor(Color.BLACK)
        }
        viewModel.getAllCountDown()?.observe(viewLifecycleOwner, Observer {
            /**
             * 这部分处理已经过去的倒计时，
             * 将它们放到列表最下
             */
            val arrayList = it as ArrayList
            val arrayListPass = ArrayList<CountDown>()
            arrayList.forEach {
                if(DateUtils.getCountDownTime(it!!.time) < 0){
                    arrayListPass.add(it)
                }
            }
            arrayListPass.forEach {
                arrayList.remove(it)
                arrayList.add(it)
            }
            countdown_recyclerview.layoutManager = StaggeredGridLayoutManager(2 ,StaggeredGridLayoutManager.VERTICAL)
            countdown_recyclerview.adapter = CountDownRecyclerAdapter(context!!, arrayList, childFragmentManager, viewModel.repository)
        })


        fab_add_countdown.setOnClickListener {
            val view = LayoutInflater.from(context)
                .inflate(R.layout.dialog_edit_count_down_location, null, false)
            val edit = view.findViewById<androidx.appcompat.widget.AppCompatEditText>(R.id.edit_count_down_location)
            val til_edit = view.findViewById<TextInputLayout>(R.id.til_count_down_location)
            edit.hint = "请输入名称"
            val dialog = DialogWithView.newInstance(
                view,
                "请填写名称",
                true,
                DialogInterface.OnClickListener { _, _ ->
                    when {
                        edit.text!!.isEmpty() -> {
                            Toasty.error(context!!, "名称不能为空~").show()
                        }
                        else -> {
                            val countDown = CountDown(edit.text.toString(), "",9999, 9999, 9999, 9999, 9999, "","0")
                            viewModel.insertCountDown(countDown)
                            Toasty.success(context!!, "保存成功", Toasty.LENGTH_SHORT).show()
                        }
                    }
                },
                null)
            dialog.show(childFragmentManager, "editLocation")

        }
    }

    override fun setContent(): Int {
        return R.layout.fragment_count_down
    }

}
